<?php    
class ControllerCustomerCustomer extends Controller { 
	private $error = array();
  
  	public function index() {
		$this->load->language('customer/customer');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('customer/customer');
    	$this->getList();
  	}
    
  	private function getList() {
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('customer/customer', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		
		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = null;
		}
		if (isset($this->request->get['filter_email'])) {
			$filter_email = $this->request->get['filter_email'];
		} else {
			$filter_email = null;
		}
		if (isset($this->request->get['filter_customer_grade_id'])) {
			$filter_customer_grade_id = $this->request->get['filter_customer_grade_id'];
		} else {
			$filter_customer_grade_id = null;
		}
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'name'; 
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$this->data['customers'] = array();
		$data = array(
			'filter_name'              => $filter_name, 
			'filter_email'             => $filter_email, 
			'filter_customer_grade_id' => $filter_customer_grade_id, 
			'sort'                     => $sort,
			'order'                    => $order,
			'start'                    => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'                    => $this->config->get('config_admin_limit')
		);
		$customer_total = $this->model_customer_customer->getTotalCustomers($data);
		$results = $this->model_customer_customer->getCustomers($data);
 
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('customer/customer/update', 'token=' . $this->session->data['token'] . '&customer_id=' . $result['customer_id'], 'SSL')
			);
			$this->data['customers'][] = array(
				'customer_id'    => $result['customer_id'],
				'name'           => $result['name'],
				'email'          => $result['email'],
				'newsletter'     => ($result['newsletter'] ? $this->language->get('text_yes') : $this->language->get('text_no')),
				'point'          => $result['point'],
				'grade_name'     => $result['grade_name'],
				'date_added'     => $result['date_added'],
				'action'         => $action
			);
		}	
					
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');	
		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_email'] = $this->language->get('column_email');
		$this->data['column_newsletter'] = $this->language->get('column_newsletter');
		$this->data['column_point'] = $this->language->get('column_point');
		$this->data['column_customer_grade'] = $this->language->get('column_customer_grade');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_action'] = $this->language->get('column_action');		
		
		$this->data['button_filter'] = $this->language->get('button_filter');

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$url = '';
		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}
		if (isset($this->request->get['filter_email'])) {
			$url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
		}
		if (isset($this->request->get['filter_customer_grade_id'])) {
			$url .= '&filter_customer_grade_id=' . $this->request->get['filter_customer_grade_id'];
		}
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_name'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&sort=name' . $url, 'SSL');
		$this->data['sort_email'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&sort=c.email' . $url, 'SSL');
		$this->data['sort_customer_grade'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&sort=c.customer_grade_id' . $url, 'SSL');
		
		$url = '';
		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}
		if (isset($this->request->get['filter_email'])) {
			$url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
		}
		if (isset($this->request->get['filter_customer_grade_id'])) {
			$url .= '&filter_customer_grade_id=' . $this->request->get['filter_customer_grade_id'];
		}
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $customer_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();

		$this->data['filter_name'] = $filter_name;
		$this->data['filter_email'] = $filter_email;
		$this->data['filter_customer_grade_id'] = $filter_customer_grade_id;
		
		$this->load->model('customer/customer_grade');
    	$this->data['customer_grades'] = $this->model_customer_customer_grade->getCustomerGrades();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		
		$this->template = 'customer/customer_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
  	}
	
  	public function update() {
		$this->load->language('customer/customer');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('customer/customer');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
    		$customer_id = $this->request->get['customer_id'];
    		$customer_grade_id = $this->request->post['customer_grade_id'];
    		$is_backlist = (int)$this->request->post['is_backlist'];
    		$reason = $this->request->post['reason'];
    		$status = 1;
    		if($is_backlist == 1 && $reason != null && strlen($reason) > 0){
    			$status = 0;
    			$this->model_customer_customer->addBlacklist($customer_id, $reason);
    		}
    		
			$this->model_customer_customer->editCustomer($customer_id, $customer_grade_id, $status);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('customer/customer', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getForm();
  	}   
  	
  	private function getForm() {
    	$this->data['heading_title'] = $this->language->get('heading_title');
 
		$this->data['text_no_results'] = $this->language->get('text_no_results');
    	$this->data['text_yes'] = $this->language->get('text_yes');
    	$this->data['text_no'] = $this->language->get('text_no');
    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
    	$this->data['text_sex_man'] = $this->language->get('text_sex_man');
    	$this->data['text_sex_woman'] = $this->language->get('text_sex_woman');
		$this->data['text_add_blacklist'] = $this->language->get('text_add_blacklist');
		$this->data['text_remove_blacklist'] = $this->language->get('text_remove_blacklist');
		
    	$this->data['entry_firstname'] = $this->language->get('entry_firstname');
    	$this->data['entry_lastname'] = $this->language->get('entry_lastname');
    	$this->data['entry_sex'] = $this->language->get('entry_sex');
    	$this->data['entry_email'] = $this->language->get('entry_email');
    	$this->data['entry_phone'] = $this->language->get('entry_phone');
    	$this->data['entry_fax'] = $this->language->get('entry_fax');
		$this->data['entry_newsletter'] = $this->language->get('entry_newsletter');
    	$this->data['entry_customer_grade'] = $this->language->get('entry_customer_grade');
    	$this->data['entry_customer_language'] = $this->language->get('entry_customer_language');
		$this->data['entry_point'] = $this->language->get('entry_point');
		$this->data['entry_register_ip'] = $this->language->get('entry_register_ip');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_date_added'] = $this->language->get('entry_date_added');
		
		$this->data['entry_country'] = $this->language->get('entry_country');
		$this->data['entry_zone'] = $this->language->get('entry_zone');
		$this->data['entry_city'] = $this->language->get('entry_city');
		$this->data['entry_address_1'] = $this->language->get('entry_address_1');
		$this->data['entry_address_2'] = $this->language->get('entry_address_2');
		$this->data['entry_postcode'] = $this->language->get('entry_postcode');
		$this->data['entry_default'] = $this->language->get('entry_default');
 
		$this->data['entry_reason'] = $this->language->get('entry_reason');
		$this->data['entry_is_backlist'] = $this->language->get('entry_is_backlist');
		
		$this->data['column_ip'] = $this->language->get('column_ip');
		$this->data['column_login_num'] = $this->language->get('column_login_num');
		$this->data['column_last_login_date'] = $this->language->get('column_last_login_date');
		
		$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
	
		$this->data['tab_general'] = $this->language->get('tab_general');
		$this->data['tab_address'] = $this->language->get('tab_address');
		$this->data['tab_login_ip'] = $this->language->get('tab_login_ip');
		$this->data['tab_backlist'] = $this->language->get('tab_backlist');

		$this->data['token'] = $this->session->data['token'];

  		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('customer/customer', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);

		$this->data['action'] = $this->url->link('customer/customer/update', 'token=' . $this->session->data['token'] . '&customer_id=' . $this->request->get['customer_id'], 'SSL');
    	$this->data['cancel'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'], 'SSL');
	
    	$this->load->model('customer/customer_grade');
		$this->data['customer_grades'] = $this->model_customer_customer_grade->getCustomerGrades();
		
		$this->load->model('localisation/country');
		$this->data['countries'] = $this->model_localisation_country->getCountries();
		
    	$customer_info = $this->model_customer_customer->getCustomer($this->request->get['customer_id']);
		$this->data['firstname'] = $customer_info['firstname'];
    	$this->data['lastname'] = $customer_info['lastname'];
		$this->data['sex'] = $customer_info['sex'];
		$this->data['email'] = $customer_info['email'];
		$this->data['phone'] = $customer_info['phone'];
		$this->data['fax'] = $customer_info['fax'];
		$this->data['newsletter'] = $customer_info['newsletter'];
		$this->data['customer_grade_id'] = $customer_info['customer_grade_id'];
		$this->data['point'] = $customer_info['point'];
		$this->data['register_ip'] = $customer_info['register_ip'];
		$this->data['status'] = $customer_info['status'];
		$this->data['date_added'] = $customer_info['date_added'];
		$this->data['language_name'] = $customer_info['language_name'];
			
		$this->data['addresses'] = $this->model_customer_customer->getAddresses($this->request->get['customer_id']);
		
		$this->data['ips'] = $this->model_customer_customer->countIpsByCustomerId($this->request->get['customer_id']);
		
		$this->template = 'customer/customer_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
					
	public function autocomplete() {
		$json = array();
		if (isset($this->request->get['filter_email'])) {
			$this->load->model('customer/customer');
			$json = $this->model_customer_customer->customerAutoComplete($this->request->get['filter_email']);
		}
		$this->response->setOutput(json_encode($json));
	}
}
?>